<template>
  <ContentWrap>
    <el-form class="-mb-15px" :model="queryParams" ref="queryFormRef" :inline="true" label-width="68px">
      <el-form-item label="题库名称" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入题库名称" clearable class="!w-240px" @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <!-- <el-button type="primary" plain @click="openForm('create')" v-hasPermi="['pm:catstore:create']">
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button> -->
        <el-button type="warning" plain @click="handleImport" v-hasPermi="['pm:catstore:import']">
          <Icon icon="ep:upload" /> 导入
        </el-button>
        <el-button type="success" plain @click="handleTemplate">
          <Icon icon="ep:download" class="mr-5px" /> 模版下载
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <ContentWrap>
    <el-table v-loading="loading" :data="list">
      <el-table-column prop="id" label="编号" align="center" width="100" />
      <el-table-column prop="name" label="题库名称" min-width="160" />
      <el-table-column prop="dimension" label="验证维度" min-width="120" />
      <el-table-column prop="questionCount" label="题目数量" align="center" width="100" />
      <el-table-column prop="enable" label="启动" align="center" width="120">
        <template #default="scope">
          <el-switch
            v-model="scope.row.enable"
            :active-value="1"
            :inactive-value="0"
            :disabled="!checkPermi(['pm:catstore:update'])"
            @change="() => handleEnableChange(scope.row)"
          />
        </template>
      </el-table-column>
      <el-table-column prop="remark" label="备注" min-width="180" />
      <el-table-column prop="createBy" label="创建人" width="120" />
      <el-table-column prop="createTime" label="创建时间" :formatter="dateFormatter" width="180" align="center" />
      <el-table-column label="操作" align="center" width="260" fixed="right">
        <template #default="scope">
          <el-button link type="primary" @click="openForm('update', scope.row.id)" v-hasPermi="['pm:catstore:update']">编辑</el-button>
          <el-button link type="danger" @click="handleDelete(scope.row.id)" v-hasPermi="['pm:catstore:delete']">删除</el-button>
          <el-button link type="primary" @click="openQuestionList(scope.row)" v-hasPermi="['pm:catstore:update']">题目列表</el-button>
        </template>
      </el-table-column>
    </el-table>
    <Pagination :total="total" v-model:page="queryParams.pageNo" v-model:limit="queryParams.pageSize" @pagination="getList" />
  </ContentWrap>

  <CatStoreImportForm ref="importFormRef" @success="getList" />
  <CatStoreForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts">
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import * as CatStoreApi from '@/api/pm/catstore'
import CatStoreImportForm from './CatStoreImportForm.vue'
import CatStoreForm from './CatStoreForm.vue'
import { checkPermi } from '@/utils/permission'
import { useRouter } from 'vue-router'


defineOptions({ name: 'PmCatStore' })

const router = useRouter()
const message = useMessage()
const loading = ref(true)
const total = ref(0)
const list = ref<any[]>([])
const queryFormRef = ref()
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  name: undefined as string | undefined
})

const getList = async () => {
  loading.value = true
  try {
    const data: any = await CatStoreApi.getCatStorePage({
      page: queryParams.pageNo,
      limit: queryParams.pageSize,
      keyWord: queryParams.name
    })
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

const resetQuery = () => {
  queryFormRef.value?.resetFields?.()
  handleQuery()
}

const importFormRef = ref()
const handleImport = () => {
  importFormRef.value.open()
}

const handleTemplate = async () => {
  const res = await CatStoreApi.downloadCatStoreTemplate()
  download.excel(res as unknown as Blob, '题库导入模版.xls')
}

const formRef = ref()
const openForm = (mode: 'create' | 'update', id?: number) => {
  formRef.value.open(mode, id)
}

const handleEnableChange = async (row: any) => {
  const previous = row.enable === 1 ? 0 : 1
  try {
    await CatStoreApi.updateCatStore({...row})
    message.success('修改成功')
  } catch {
    // 失败回滚
    row.enable = previous
  }
}

const openQuestionList = (row: any) => {
  router.push({ path: '/pm/pmtm', query: { storeId: row.id ,storeName: row.name} })
}

const handleDelete = async (id: number) => {
  try {
    await message.delConfirm()
    await CatStoreApi.deleteCatStore(id)
    message.success('删除成功')
    await getList()
  } catch {}
}

onMounted(() => getList())
</script>


